use core::panic;

#[derive(Clone, Copy)]
enum WorkerType {
    Female50,
    Female55,
    MaleWorker,
}

pub fn retire_time(time: &str, tp: &str) -> String {
    let (year, month) = time.split_once('-').unwrap();
    let year: u32 = year.parse().unwrap();
    let month: u32 = month.parse().unwrap();

    let tp = match tp {
        "男职工" => WorkerType::MaleWorker,
        "原法定退休年龄55周岁女职工" => WorkerType::Female55,
        "原法定退休年龄50周岁女职工" => WorkerType::Female50,
        _ => panic!("invalid worker type"),
    };
    let bounds = match tp {
        WorkerType::MaleWorker => (1965, 1977),
        WorkerType::Female55 => (1970, 1982),
        WorkerType::Female50 => (1975, 1985),
    };
    let extra_month = if year < bounds.0 {
        0
    } else if year >= bounds.1 {
        match tp {
            WorkerType::MaleWorker => 36,
            WorkerType::Female55 => 36,
            WorkerType::Female50 => 60,
        }
    } else {
        ((year - bounds.0) * 12 + month).div_ceil(4)
    };

    let base_year = match tp {
        WorkerType::MaleWorker => 60,
        WorkerType::Female55 => 55,
        WorkerType::Female50 => 50,
    };
    let total_month = base_year * 12 + extra_month;

    let new_month = total_month + month - 1;
    let new_year = year + new_month / 12;
    let new_month = new_month % 12 + 1;

    format!(
        "{new_year:04}-{new_month:02},{},{extra_month}",
        ((total_month as f32 / 12.) * 100.).round() / 100.
    )
}
